package com.whlilong.jxc.editor.support;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;

import com.whlilong.jxc.listener.IEditorListener;
import com.whlilong.jxc.listener.ITextKeyChangeListener;
import com.whlilong.jxc.model.Depart;
import com.whlilong.jxc.model.Yginfo;
import com.whlilong.jxc.sys.ExtentionInfo;

public class StaffInfoEditorSupport extends EditingSupport implements
		ITextKeyChangeListener {

	private List<Depart> departInfoList = new ArrayList<Depart>();
	private String[] departItems;
	private int currentColumn;
	private CellEditor editor;
	private boolean isFirst = true;

	public StaffInfoEditorSupport(TableViewer viewer, int column,
			IEditorListener listener) {
		super(viewer);
		departInfoList = ExtentionInfo.getInstance().getDepartInfoList();
		departItems = new String[departInfoList.size() + 1];
		departItems[0] = "不定";
		for (int i = 0; i < departInfoList.size(); i++) {
			departItems[i + 1] = departInfoList.get(i).getDepartname();
		}
		this.currentColumn = column;
		switch (currentColumn) {
		case 2:
		case 3:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
			editor = new StaffInfoTextKeyCellEditor(viewer.getTable(),
					listener, column, this);
			break;
		case 4:
			editor = new StaffInfoComboBoxCellEditor(viewer.getTable(),
					listener, column, this, departItems);
			break;
		default:
			editor = null;
		}
	}

	@Override
	protected boolean canEdit(Object element) {
		// TODO Auto-generated method stub
		return (currentColumn != 1);
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		// TODO Auto-generated method stub
		return editor;
	}

	@Override
	protected Object getValue(Object element) {
		Yginfo obj = (Yginfo) element;
		switch (currentColumn) {
		case 2:
			String id = obj.getYgid();
			if (isFirst) {
				((StaffInfoTextKeyCellEditor) editor).setOldString(id);
				isFirst = false;
			}
			return id;
		case 3:
			return obj.getYgname();
		case 4:
			for (int i = 0; i < departInfoList.size(); i++)
				if (obj.getPartname().equals(
						departInfoList.get(i).getDepartname()))
					return i + 1;
			return 0;
		case 5:
			return obj.getXb();
		case 6:
			return obj.getStatus();
		case 7:
			return obj.getCardid();
		case 8:
			return obj.getTag();
		case 9:
			return obj.getDate();
		case 10:
			return obj.getZwname();
		case 11:
			return obj.getBirthday();
		case 12:
			return obj.getXlname();
		case 13:
			return obj.getTel();
		case 14:
			return obj.getAddress();
		case 15:
			return obj.getJdgz().toString();

		}
		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		Yginfo obj = (Yginfo) element;
		switch (currentColumn) {
		case 2:
			obj.setYgid(String.valueOf(value));
			break;
		case 3:
			obj.setYgname(String.valueOf(value));
			break;

		case 4:
			if (Integer.valueOf(String.valueOf(value)) == 0)
				obj.setPartname("不定");
			for (int i = 0; i < departInfoList.size(); i++)
				if (Integer.valueOf(String.valueOf(value)) == i + 1)
					obj.setPartname(departInfoList.get(i).getDepartname());
			break;

		case 5:
			obj.setXb(String.valueOf(value));
			break;

		case 6:
			obj.setStatus(String.valueOf(value));
			break;

		case 7:
			obj.setCardid(String.valueOf(value));
			break;

		case 8:
			obj.setTag(String.valueOf(value));
			break;

		case 9:
			obj.setDate(String.valueOf(value));
			break;

		case 10:
			obj.setZwname(String.valueOf(value));
			break;

		case 11:
			obj.setBirthday(String.valueOf(value));
			break;

		case 12:
			obj.setXlname(String.valueOf(value));
			break;

		case 13:
			obj.setTel(String.valueOf(value));
			break;

		case 14:
			obj.setAddress(String.valueOf(value));
			break;

		case 15:
			obj.setJdgz(BigDecimal.valueOf(Double.valueOf(value.toString())));
			break;
		}

		getViewer().update(element, null);
	}

	@Override
	public void changeEvent() {
		if (editor instanceof StaffInfoTextKeyCellEditor)
			((StaffInfoTextKeyCellEditor) editor).releaseKey();
		if (editor instanceof StaffInfoComboBoxCellEditor)
			((StaffInfoComboBoxCellEditor) editor).releaseKey();
	}

	@Override
	public void modify() {
		isFirst = true;
	}

}
